package view;

import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import darksideoftheloom.Event;

/**
 * This frame handles registration of users.
 * 
 * @author Shane Kwon
 * @version 2013-03-09
 * 
 */
@SuppressWarnings("serial")
public class BeweaveRegisterPanel extends JPanel {

    /**
     * The width of the panel.
     */
    private static final int WIDTH = 1024;

    /**
     * The height of the panel.
     */
    private static final int HEIGHT = 768;

    /**
     * The default width of textfields in columns.
     */
    private static final int COLS = 20;

    /**
     * Constructs a panel for the user to fill out registration info.
     * 
     * @param the_weavent
     *            The weaving Event
     * @param the_panel
     *            the panel container, used for switching between panels
     */
    public BeweaveRegisterPanel(final Event the_weavent, final JPanel the_panel) {
        super();
        setLayout(null);

        final JLabel namelabel = new JLabel("Name: ");
        final JLabel usernamelabel = new JLabel("User name: ");
        final JLabel passwordlabel = new JLabel("Password: ");
        final JLabel phonelabel = new JLabel("Phone number: ");
        final JLabel addresslabel = new JLabel("Address: ");
        final JLabel emaillabel = new JLabel("Email address: ");

        final JTextField namefield = new JTextField();
        final JTextField usernamefield = new JTextField();
        final JTextField passwordfield = new JTextField();
        final JTextField phonefield = new JTextField();
        final JTextField addressfield = new JTextField();
        final JTextField emailfield = new JTextField();

        namefield.setColumns(COLS);
        usernamefield.setColumns(COLS);
        passwordfield.setColumns(COLS);
        phonefield.setColumns(COLS);
        addressfield.setColumns(COLS);
        emailfield.setColumns(COLS);

        final JButton registerbutton = new JButton(new AbstractAction(
                "Register") {
            public void actionPerformed(final ActionEvent the_event) {
                the_weavent.addAttendee(namefield.getText(),
                        phonefield.getText(), addressfield.getText(),
                        emailfield.getText(), usernamefield.getText(),
                        passwordfield.getText());
                // TODO figure out saving problem (nullpointerexception in
                // DataStorage)
                if (the_weavent.getValidity()) {
                	the_weavent.save();
                    for (Component c : BeweaveRegisterPanel.this.getComponents()) {
                        c.setEnabled(false);
                    }
                    JOptionPane.showMessageDialog(BeweaveRegisterPanel.this,
                            "Thanks for registering!");
                    CardLayout cl = (CardLayout) the_panel.getLayout();
                    cl.show(the_panel, "login");
                    for (Component c : BeweaveRegisterPanel.this.getComponents()) {
                        if (c instanceof JTextField) {
                            ((JTextField) c).setText("");
                        }
                        c.setEnabled(true);
                    }
                } else {
                	JOptionPane.showMessageDialog(BeweaveRegisterPanel.this,
                            "Registration fields incompatable.");
                }
                
            }
        });
        final JButton backbutton = new JButton(new AbstractAction("Cancel") {
            public void actionPerformed(final ActionEvent the_event) {
                CardLayout cl = (CardLayout) the_panel.getLayout();
                cl.show(the_panel, "login");
            }
        });

        add(namelabel);
        add(namefield);
        add(usernamelabel);
        add(usernamefield);
        add(passwordlabel);
        add(passwordfield);
        add(phonelabel);
        add(phonefield);
        add(addresslabel);
        add(addressfield);
        add(emaillabel);
        add(emailfield);
        add(registerbutton);
        add(backbutton);

        // TODO get rid of hardcoded values
        Insets insets = getInsets();
        Dimension size = namelabel.getPreferredSize();
        namelabel.setBounds(300 + insets.left, 225 + insets.top, size.width,
                size.height);
        size = usernamelabel.getPreferredSize();
        usernamelabel.setBounds(300 + insets.left, 250 + insets.top,
                size.width, size.height);
        size = passwordlabel.getPreferredSize();
        passwordlabel.setBounds(300 + insets.left, 275 + insets.top,
                size.width, size.height);
        size = phonelabel.getPreferredSize();
        phonelabel.setBounds(300 + insets.left, 300 + insets.top, size.width,
                size.height);
        size = addresslabel.getPreferredSize();
        addresslabel.setBounds(300 + insets.left, 325 + insets.top, size.width,
                size.height);
        size = emaillabel.getPreferredSize();
        emaillabel.setBounds(300 + insets.left, 350 + insets.top, size.width,
                size.height);

        size = namefield.getPreferredSize();
        namefield.setBounds(500 + insets.left, 225 + insets.top, size.width,
                size.height);
        size = usernamefield.getPreferredSize();
        usernamefield.setBounds(500 + insets.left, 250 + insets.top,
                size.width, size.height);
        size = passwordfield.getPreferredSize();
        passwordfield.setBounds(500 + insets.left, 275 + insets.top,
                size.width, size.height);
        size = phonefield.getPreferredSize();
        phonefield.setBounds(500 + insets.left, 300 + insets.top, size.width,
                size.height);
        size = addressfield.getPreferredSize();
        addressfield.setBounds(500 + insets.left, 325 + insets.top, size.width,
                size.height);
        size = emailfield.getPreferredSize();
        emailfield.setBounds(500 + insets.left, 350 + insets.top, size.width,
                size.height);

        size = registerbutton.getPreferredSize();
        registerbutton.setBounds(540 + insets.left, 400 + insets.top,
                size.width, size.height);
        size = backbutton.getPreferredSize();
        backbutton.setBounds(642 + insets.left, 400 + insets.top, size.width,
                size.height);

        setPreferredSize(new Dimension(WIDTH, HEIGHT));
    }
}
